package cn.yunhe.servlet;

import cn.yunhe.entity.ProductCategory;
import cn.yunhe.service.ProductCategoryService;
import cn.yunhe.util.JsonUtil;
import cn.yunhe.util.PageUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@WebServlet("/GetAllCategoryServlet")
public class GetAllCategoryServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //String keyword = request.getParameter("keyword");
        int pageIndex = Integer.parseInt(request.getParameter("pageIndex"));

        ProductCategoryService pcs = new ProductCategoryService();
        List<ProductCategory>list = pcs.getCategoryByPage(pageIndex);
        request.setAttribute("list",list);
        int i = pcs.getCategoryCount();
        int totalPage = PageUtil.getTotalPages(i);

        Map<String, Object> maps = new HashMap<String, Object>();
        maps.put("list", list);
        maps.put("totalPage",totalPage);
        maps.put("pageIndex",pageIndex);

        JsonUtil.getJson(response, maps);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }
}
